package com.scoy.honeymei.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.activity.mall.SearchHomeActivity;
import com.scoy.honeymei.adapter.TravelAdapter;
import com.scoy.honeymei.adapter.TravelLeftAdapter;
import com.scoy.honeymei.adapter.TravelRightAdapter;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.TravelBean;
import com.scoy.honeymei.bean.TravelSignBean;
import com.scoy.honeymei.bean.TravelTypeBean;
import com.scoy.honeymei.databinding.ActivityTravel0ListBinding;
import com.scoy.honeymei.databinding.ActivityTravelMainBinding;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.ApiCallBack;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.GlideImgUtil;
import com.scoy.honeymei.utils.MyUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;
import com.zhy.view.flowlayout.TagView;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: TravelActivity$
 * @Description: 旅游界面
 * @Author: GaoZhan
 * @CreateDate: 2019/9/6/0006$ 下午 14:16$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/6/0006$ 下午 14:16$
 * @UpdateRemark: 更新内容
 */
public class Travel0Activity extends BaseActivity {

    private Travel0Activity mContext;
    private TravelAdapter travelAdapter;
    private TravelLeftAdapter leftAdapter;
    private ActivityTravelMainBinding binding;
    private TravelRightAdapter rightAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityTravelMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ButterKnife.bind(this);
        mContext = Travel0Activity.this;
        binding.hfSearchTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(Travel0Activity.this, SearchHomeActivity.class);
                intent.putExtra("type",1);
                startActivity(intent);
            }
        });
        initNormal();

        httpLeft();

        httpTravelSign();
    }

    @Override
    public void initNormal() {
        binding.layoutTop.titleTv.setText(R.string.travel);
        MyUtil.setStatusBar(mContext, getWindow(), true, R.color.colorWhite);
        binding.layoutTop.titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);

        binding.layoutTop.backIv.setOnClickListener(v -> onBackPressed());

        initLeftRv();
        initRightRv();

        initSrl();

    }

    private void initSrl() {
        binding.atmSrl.setOnRefreshListener(refreshLayout -> {
            refreshLayout.finishRefresh(1500);
            rightAdapter.clearData();
            httpTravelList(0);
        }).setOnLoadMoreListener(refreshLayout -> {
            refreshLayout.finishLoadMore(1500);
            httpTravelList(pageInt);
        });

    }

    private void httpLeft() {
        HttpParams params = new HttpParams();
        HpGo.newInstance().HttpGo(mContext, MyUrl.TRAVEL_TYPE, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                List<TravelTypeBean> data = new Gson().fromJson(o, new TypeToken<List<TravelTypeBean>>() {
                }.getType());
                if (data != null && data.size() > 0) {
                    TravelTypeBean travelTypeBean = data.get(0);
                    travelTypeBean.setCheck(1);
                    typeId = travelTypeBean.getId();
                    if (travelTypeBean.getImage() != null && !"".equals(travelTypeBean.getImage())) {
                        GlideImgUtil.glidePic(mContext, travelTypeBean.getImage(), binding.atmRighttopIv);
                        binding.atmRighttopIv.setVisibility(View.VISIBLE);
                    } else {
                        binding.atmRighttopIv.setVisibility(View.GONE);
                    }
                    rightAdapter.clearData();
                    httpTravelList(0);
                }
                leftAdapter.setNewData(data);
            }
        });

    }

    String tagId = "";
    private int typeId = 0;
    private int pageInt = 1;

    private void httpTravelList(int myPage) {
        if (myPage == -1) {
            MyUtil.mytoast(mContext, "没有更多了~");
            return;
        }
        HttpParams params = new HttpParams();
        params.put("page", myPage);
        params.put("type", typeId);
        params.put("tag_id", tagId);
        HpGo.newInstance().HttpGo(mContext, MyUrl.TRAVEL_LIST, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                List<TravelBean> data = new Gson().fromJson(o, new TypeToken<List<TravelBean>>() {
                }.getType());
                rightAdapter.addData(data);
                pageInt = data.size() == 0 ? -1 : myPage + 1;
                binding.nodataTv.setVisibility(rightAdapter.getData().size() == 0 ? View.VISIBLE : View.GONE);
            }

        });

    }

    private void httpTravelSign() {
        HttpParams params = new HttpParams();
        HpGo.newInstance().HttpGo(mContext, MyUrl.TRAVEL_SIGN, params, new ApiCallBack() {
            @Override
            public void onSuccess(String t) {
                List<TravelSignBean> data = new Gson().fromJson(t, new TypeToken<List<TravelSignBean>>() {
                }.getType());

                if (data != null && data.size() > 0) {
                    binding.adgSignFl.setVisibility(View.VISIBLE);

                    //往容器内添加TextView数据
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
                    layoutParams.setMargins(10, 2, 10, 2);

                    binding.adgSignFl.removeAllViews();

                    binding.adgSignFl.setAdapter(new TagAdapter<TravelSignBean>(data) {
                        @Override
                        public View getView(FlowLayout parent, int position, TravelSignBean s) {
                            TextView tv = new TextView(mContext);
                            tv.setPadding(28, 4, 28, 4);
                            tv.setText(s.getName());
                            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                            //    tv.setMaxEms(10);
                            //    tv.setSingleLine();
                            tv.setTextColor(getResources().getColor(R.color.color_white_gray));
                            tv.setBackgroundResource(R.drawable.selector_blue_graye);
                            tv.setLayoutParams(layoutParams);
                            return tv;
                        }

                        @Override
                        public void onSelected(int position, View view) {
                            super.onSelected(position, view);
                            TextView view1 = (TextView) view;
                            view1.setTextColor(getResources().getColor(R.color.color_white_gray));
                        }

                        @Override
                        public void unSelected(int position, View view) {
                            super.unSelected(position, view);
                            TextView view1 = (TextView) view;
                            view1.setTextColor(getResources().getColor(R.color.color_white_gray));
                        }
                    });

                    binding.adgSignFl.setOnTagClickListener((view, position, parent) -> {
                        TagView view1 = (TagView) view;
                        TravelSignBean travelSignBean = data.get(position);
                        tagId = view1.isChecked() ? travelSignBean.getId() + "" : "";
                        rightAdapter.clearData();
                        httpTravelList(0);

                        return false;
                    });

                } else {
                    binding.adgSignFl.setVisibility(View.GONE);
                }
            }
        });

    }

    private void initLeftRv() {
        leftAdapter = new TravelLeftAdapter(mContext);
        binding.atmLeftRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        binding.atmLeftRv.setAdapter(leftAdapter);
        leftAdapter.setOnOneClick(pos -> {

            List<TravelTypeBean> data = leftAdapter.getData();
            for (int i = 0; i < data.size(); i++) {
                TravelTypeBean item = data.get(i);
                if (item.getCheck() == 1) {
                    if (pos == i) {
                        return;
                    }
                    item.setCheck(0);
                    leftAdapter.notifyItemChanged(i);
                }
            }
            TravelTypeBean travelTypeBean = leftAdapter.getItem(pos);

            typeId = travelTypeBean.getId();
            if (travelTypeBean.getImage() != null && !"".equals(travelTypeBean.getImage())) {
                GlideImgUtil.glidePic(mContext, travelTypeBean.getImage(), binding.atmRighttopIv);
                binding.atmRighttopIv.setVisibility(View.VISIBLE);
            } else {
                binding.atmRighttopIv.setVisibility(View.GONE);
            }
            rightAdapter.clearData();
            httpTravelList(0);

            travelTypeBean.setCheck(1);
            leftAdapter.notifyItemChanged(pos);
        });

    }

    private void initRightRv() {
        rightAdapter = new TravelRightAdapter(mContext);
        binding.atmRightRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        DividerItemDecoration did = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
        did.setDrawable(getResources().getDrawable(R.drawable.divider_gray_line_pd));
        binding.atmRightRv.addItemDecoration(did);
        binding.atmRightRv.setAdapter(rightAdapter);
        rightAdapter.setOnOneClick(pos -> {
            TravelBean travelBean = rightAdapter.getItem(pos);
            Intent intent = new Intent(mContext, TravelDetailActivity.class);
            intent.putExtra("tid", travelBean.getId());
            startActivity(intent);
        });
    }


}
